package com.itcv.redis.service.impl;

import com.itcv.redis.basic.PatientInfo;
import com.itcv.redis.dao.PatientInfoMapper;
import com.itcv.redis.query.PatientQuery;
import com.itcv.redis.service.PatientInfoService;
import com.itcv.redis.util.GenerateNameUtil;
import com.itcv.redis.util.IdCardGeneratorUtil;
import com.itcv.redis.util.ObjectUtils;
import com.itcv.redis.util.PrimaryKeyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class PatientInfoServiceImpl implements PatientInfoService {

    private static Logger logger = LoggerFactory.getLogger(PatientInfoService.class);

    @Autowired
    private PatientInfoMapper patientInfoMapper;
    @Autowired
    private PrimaryKeyUtil primaryKeyUtil;
    @Override
    public int batchInsertPatientInfo(List<PatientInfo> patientInfoList) throws Exception {
        logger.info("开始初始化患者信息"+System.currentTimeMillis());
        Long startTime = System.currentTimeMillis();
        int num =1;
        for(int i=0;i<100000;i++){
            try {
                PatientInfo patientInfo = new PatientInfo();
                patientInfo.setIdentificationNum(IdCardGeneratorUtil.generate());
                patientInfo.setPatientName(GenerateNameUtil.generateName());
                String identificationNum = patientInfo.getIdentificationNum();
                if(identificationNum.length()>14){
                    String bornStr = identificationNum.substring(6,14);
                    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
                    Date date = dateFormat.parse(bornStr);
                    patientInfo.setBornDate(date);
                }else{
                    logger.info("记录一下这个身份证号{}"+identificationNum);
                    continue;
                }

                Random random = new Random();
                Byte b = Byte.valueOf(String.valueOf(random.nextInt(2)+1));
                patientInfo.setPatientSex(b);
                String prefix = primaryKeyUtil.getOrderIdPrefix(new Date());
                String globaleId = prefix+primaryKeyUtil.orderId(prefix);
                patientInfo.setGlobalId(globaleId);
                patientInfoMapper.insert(patientInfo);
                num++;
            } catch (ParseException e) {
                e.printStackTrace();
                logger.error("初始化数据执行异常",e);
            }

        }
        Long endTime = System.currentTimeMillis();
        logger.info("结束初始化患者信息，"+num+"条数据共耗时"+(endTime-startTime)+"毫秒");
        logger.info("结束初始化患者信息，"+num+"条数据共耗时"+(endTime-startTime)/1000+"秒");
        logger.info("结束初始化患者信息，"+num+"条数据共耗时"+(endTime-startTime)/1000/60+"分钟");

        return 0;
    }

    @Override
    public int insertPatientInfo(PatientInfo patientInfo) throws Exception {
        return patientInfoMapper.insert(patientInfo);
    }

    @Override
    public List<PatientInfo> selectPatientList(PatientQuery patientQuery) throws Exception {
       List<PatientInfo> patientInfoList = patientInfoMapper.queryPatientList(patientQuery);
       return patientInfoList;
    }
    @Override
    public void matchByDb(List<PatientInfo> patientInfos){
        logger.info("当前需要匹配的数据条数为{}",patientInfos.size());
        long startTime = System.currentTimeMillis();
        logger.info("开始进行匹配{}",startTime);
        if(ObjectUtils.notEmpty(patientInfos)){
            for(PatientInfo patientInfo : patientInfos){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                PatientQuery query = new PatientQuery();
                query.setBornDate(sdf.format(patientInfo.getBornDate()));
                query.setPatientName(patientInfo.getPatientName());
                List<PatientInfo> patientInfoList = patientInfoMapper.queryPatientList(query);
                if(ObjectUtils.notEmpty(patientInfoList)){
                    logger.info("当前数据匹配上了");
                }
            }
        }
        long endTime = System.currentTimeMillis();
        logger.info("匹配结束{}",startTime);
        logger.info("匹配结束"+(endTime-startTime)+"毫秒");
        logger.info("匹配结束"+(endTime-startTime)/1000+"秒");
        logger.info("匹配结束"+(endTime-startTime)/1000/60+"分钟");
    }

    public void matchByMemory(){

    }
}
